package com.book.service.impl;

import com.book.dao.UserMapper;
import com.book.service.UserService;
import com.book.utils.MybatisUtil;
import com.book.entity.User;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl implements UserService {
    @Override
    public boolean auth(String username, String password, HttpSession session) {
        try(SqlSession sqlSession = MybatisUtil.getSession()){
            UserMapper mapper =  sqlSession.getMapper(UserMapper.class);  // sqlSession根据Mapper接口创建一个mapper对象
            User user = mapper.getUser(username, password);  // 通过mapper对象可以调用接口中方法查询数据库
            if(user == null) return false;
            session.setAttribute("user",user);  // 将查询到的用户存入服务器的session中
            return true;
        }
    }

}
